package com.bhs.sansonglogistics.ui.orderDispose;

import static com.bhs.sansonglogistics.base.Constants.serviceTel;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.AbnormalOrderDetailBean;
import com.bhs.sansonglogistics.bean.OrderScheduleBean;
import com.bhs.sansonglogistics.bean.event.OrderScheduleEvent;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.PictureShowAdapter3;
import com.bhs.sansonglogistics.ui.wallet.PayDeskActivity;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

//异常单处理进度
public class AbnormalOrderScheduleFragment extends BaseFragment implements View.OnClickListener, NetCallBack {

    private ImageView mIvStatus;
    private TextView mTvStatus;
    private Button mBtnDispose;
    private RecyclerView mRvList;
    private BaseQuickAdapter<OrderScheduleBean, BaseViewHolder> mAdapter;
    private LinearLayout mLlTopStatus;

    private int id;

    public static AbnormalOrderScheduleFragment newInstance(Bundle bundle) {
        AbnormalOrderScheduleFragment fragment = new AbnormalOrderScheduleFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_abnormal_order_schedule;
    }

    @Override
    public void initView(View root) {
        id = getArguments().getInt("id");
        mIvStatus = root.findViewById(R.id.iv_status);
        mTvStatus = root.findViewById(R.id.tv_status);
        mBtnDispose = root.findViewById(R.id.btn_dispose);
        mRvList = root.findViewById(R.id.rv_list);
        mLlTopStatus = root.findViewById(R.id.ll_top_status);
        mBtnDispose.setOnClickListener(this);
        root.findViewById(R.id.ll_service).setOnClickListener(this);
    }

    @Override
    public void initData() {
        mAdapter = new BaseQuickAdapter<OrderScheduleBean, BaseViewHolder>(R.layout.item_order_schedule) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, OrderScheduleBean item) {
                helper.setText(R.id.tv_time, DateUtils.getTime(item.getHandle_time()));
                helper.setText(R.id.tv_conductor, item.getHandle_people());
                helper.setText(R.id.tv_money, String.format("%s元", item.getHandle_money()));
                helper.setText(R.id.tv_content, item.getHandle_content());
                helper.setVisible(R.id.bottom_line, helper.getLayoutPosition() != mData.size() - 1);
                MyUtils.getAbnormalStatus(helper.getView(R.id.tv_status), item.getStatus());
                if (!TextUtils.isEmpty(item.getHandle_imgs())) {
                    LogUtils.d(item.getHandle_imgs());
                    Object[] images = item.getHandle_imgs().split(",");
                    PictureShowAdapter3 showAdapter3 = new PictureShowAdapter3(Arrays.asList(images));
                    RecyclerView rvList = helper.getView(R.id.rv_list);
                    rvList.setAdapter(showAdapter3);
                    rvList.setLayoutManager(new GridLayoutManager(getContext(), 3));
                    showAdapter3.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            new XPopup.Builder(getContext())
                                    .asImageViewer(view.findViewById(R.id.iv_picture), position, Arrays.asList(images), null, new SmartGlideImageLoader())
                                    .isShowSaveButton(false)
                                    .show();
                        }
                    });
                }
                helper.setGone(R.id.rv_list, !TextUtils.isEmpty(item.getHandle_imgs()));
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
    }


    @Override
    public void onResume() {
        super.onResume();
        loadingPopupShow();
        networkRequest(netApi.get_abnormal_info(id), this);
    }

    private OrderScheduleEvent data;



    private void setData(OrderScheduleEvent event) {
        data = event;
        if (!TextUtils.isEmpty(event.getHandle())) {
            Type type = new TypeToken<List<OrderScheduleBean>>() {
            }.getType();
            List<OrderScheduleBean> list = new Gson().fromJson(event.getHandle(), type);
            list.get(0).setStatus(event.status);
            mAdapter.setNewData(list);
            mLlTopStatus.setVisibility(View.VISIBLE);
            if (event.status.equals("2")) {
                mBtnDispose.setVisibility(View.GONE);
                mIvStatus.setImageResource(R.mipmap.ic_await);
                mTvStatus.setText("等待货主反馈");
            } else if (event.status.equals("3")) {
                mBtnDispose.setVisibility(View.VISIBLE);
                mBtnDispose.setText("去处理");
                mBtnDispose.setBackgroundColor(Color.parseColor("#495EF4"));
                mIvStatus.setImageResource(R.mipmap.ic_reject);
                mTvStatus.setText("客户驳回处理结果");
            } else if (event.status.equals("5")) {
                mBtnDispose.setVisibility(View.VISIBLE);
                mBtnDispose.setText("去支付");
                mBtnDispose.setBackgroundColor(Color.parseColor("#00AA3A"));
                mIvStatus.setImageResource(R.mipmap.ic_await);
                mTvStatus.setText("客户已同意赔偿");
            } else if (event.status.equals("7")) {
                mBtnDispose.setVisibility(View.GONE);
                mIvStatus.setImageResource(R.mipmap.ic_accomplish);
                mTvStatus.setText("受理已完成");
            } else {
                mBtnDispose.setVisibility(View.GONE);
                mIvStatus.setImageResource(R.mipmap.ic_revocation2);
                mTvStatus.setText("客户投诉已撤销");
            }
        } else {
            mLlTopStatus.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.ll_service) {
            MyUtils.callPhone(getContext(), serviceTel);
        }else {
            if (mBtnDispose.getText().toString().equals("去处理")) {
                startActivity(new Intent(getContext(), AbnormalOrderDisposeActivity.class)
                        .putExtra("id", data.getId()));
            } else if (mBtnDispose.getText().toString().equals("去支付")) {
                startActivity(new Intent(getContext(), PayDeskActivity.class)
                        .putExtra("money", data.getMoney())
                        .putExtra("type", 4)
                        .putExtra("pay_sn", String.valueOf(data.getId())));
            }
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        loadingPopupDismiss();
        if (requestCode == 0) {
            AbnormalOrderDetailBean detailBean = new Gson().fromJson(json, AbnormalOrderDetailBean.class);
            if (detailBean.isStatus()) {
                OrderScheduleEvent orderScheduleEvent = new OrderScheduleEvent(detailBean.getData().getHandle_log(),
                        detailBean.getData().getStatus(), detailBean.getData().getId(), detailBean.getData().getHandle_money());
                setData(orderScheduleEvent);
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}
